package com.zzyl.nursing.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.BedVo;
import com.zzyl.nursing.vo.DeviceInfo;
import com.zzyl.nursing.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    @Override
    public RoomVo selectRoomVoById(Long id) {
        return roomMapper.selectRoomVoById(id);
    }


    /**
     * 根据楼层id获取有智能设备的房间信息和房间中的床位信息
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
        List<RoomVo> roomVos=roomMapper.getRoomsWithDeviceByFloorId(floorId);
        //遍历每个房间
        roomVos.forEach(roomVo->{
            //获取到房间设备集合
            List<DeviceInfo> deviceList=roomVo.getDeviceVos();
            //获取到每个设备的iotId  根据iotId查询到daviceDataVos(这个数据存在redis中)
            deviceList.forEach(deviceInfo->{
                //获取到设备iotId
                String iotId = deviceInfo.getIotId();
                //根据iotId从redis中查到设备详细数据(deviceDataVos)
                String JsonStr = (String)redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, iotId);
                //判空
                if(StringUtils.isEmpty(JsonStr))
                {
                    return;//结束循环 并不是结束整个方法
                }
                //设置房间设备中的详细设备数据
                deviceInfo.setDeviceDataVos(JSONUtil.toList(JsonStr,DeviceData.class));

            });

            //获取到房间中床位集合
            List<BedVo> bedVos = roomVo.getBedVoList();
            bedVos.forEach(bedVo->{
               //获取到床位设备集合
                List<DeviceInfo> deviceVos = bedVo.getDeviceVos();
                //遍历每个房间设备
                deviceVos.forEach(deviceVo->{
                    //获取到设备iotId
                    String iotId = deviceVo.getIotId();
                    //根据整个iotId就能够从redis中获取到设备详细数据
                    String JsonStr = (String)redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, iotId);
                    //判空
                    if(StringUtils.isEmpty(JsonStr))
                    {
                        return;//跳出循环
                    }
                    //封装deviceDataVos这个是房间中每个床位设备中的详细设备数据
                    deviceVo.setDeviceDataVos(JSONUtil.toList(JsonStr,DeviceData.class));

                });


            });


        });
        return roomVos;
    }
}
